<?php

namespace IDock\Insurance\DAJIA;

use DateTime;
use Exception;
use IDock\IDock\IDockInterface;
use IDock\Insurance\DAJIA\Request\BusinessOrderNoQueryRequest;
use IDock\Insurance\DAJIA\Request\CancelOrderRequest;
use IDock\Insurance\DAJIA\Request\DeleteOrderRequest;
use IDock\Insurance\DAJIA\Request\ElectronicInsuranceDownloadRequest;
use IDock\Insurance\DAJIA\Request\ElectronicSignFileQueryRequest;
use IDock\Insurance\DAJIA\Request\ElectronicSignQueryRequest;
use IDock\Insurance\DAJIA\Request\InsuranceConfirmRequest;
use IDock\Insurance\DAJIA\Request\InsuranceOrderQueryRequest;
use IDock\Insurance\DAJIA\Request\InsuranceOrderTrackQueryRequest;
use IDock\Insurance\DAJIA\Request\InsurancePriceQueryRequest;
use IDock\Insurance\DAJIA\Request\NonCarOrderQueryRequest;
use IDock\Insurance\DAJIA\Request\NonCarUnderWritingRequest;
use IDock\Insurance\DAJIA\Request\OrderQueryRequest;
use IDock\Insurance\DAJIA\Request\RenewalInquiryRequest;
use IDock\Insurance\DAJIA\Request\UnIndividualPaymentApplyRequest;
use IDock\Insurance\DAJIA\Request\UploadFileRequest;
use IDock\Insurance\DAJIA\Request\ValidateCarRequest;
use IDock\Insurance\DAJIA\Request\VehicleActualValueRequest;
use IDock\Insurance\DAJIA\Request\VehicleAndCustomerInfoConfirmRequest;
use IDock\Insurance\DAJIA\Request\VehicleModelRequest;
use IDock\Insurance\DAJIA\Response\CancelOrderResponse;
use IDock\Insurance\DAJIA\Response\CommonResponse;
use IDock\Insurance\DAJIA\Response\ElectricalInsuranceResponse;
use IDock\Insurance\DAJIA\Response\InsuranceConfirmResponse;
use IDock\Insurance\DAJIA\Response\InsuranceOrderQueryResponse;
use IDock\Insurance\DAJIA\Response\InsurancePriceQueryResponse;
use IDock\Insurance\DAJIA\Response\NonCarOrderQueryResponse;
use IDock\Insurance\DAJIA\Response\NonCarProductListResponse;
use IDock\Insurance\DAJIA\Response\NonCarUnderWritingResponse;
use IDock\Insurance\DAJIA\Response\OrderQueryResponse;
use IDock\Insurance\DAJIA\Response\PolicyPaymentResponse;
use IDock\Insurance\DAJIA\Response\PreInquiryResponse;
use IDock\Insurance\DAJIA\Response\RenewalInquiryResponse;
use IDock\Insurance\DAJIA\Response\UploadFileResponse;
use IDock\Insurance\DAJIA\Response\ValidateCarResponse;
use IDock\Insurance\DAJIA\Response\VehicleActualValueResponse;
use IDock\Insurance\DAJIA\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarPaymentVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 大家保险
 */
class DAJIAConstruct extends Builder implements IDockInterface
{

    protected $config = [];


    public function __construct($config)
    {
        $this->config = $config;

        $this->channelCode = $config['channelCode'] ?? '';
        $this->channelPwd = $config['channelPwd'] ?? '';
        $this->secretKey = $config['secretKey'] ?? '';
        $this->deviceType = $config['deviceType'] ?? '';
        $this->interCode = '';
        $this->transTime = date('Y-m-d H:i:s');
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);

        if ($response->printer and $response->printer->toArray()) {
            return $response;
        } else {
            $request->switch = false;
            /*** @var VehicleModelResponse $response */
            $response = $this->execute($request);
        }

        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var VehicleActualValueResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 获取业务订单号接口
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PreInquiryResponse
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PreInquiryResponse
    {
        $request = new BusinessOrderNoQueryRequest();
        $request->setVisitor($premiumInquiryVisitor);
        /*** @var PreInquiryResponse $response */
        $response = $this->execute($request);

        $premiumInquiryVisitor->getExtInfoVisitor()->argc['orderNo'] = $response->printerInit()->printer->argc['orderNo'];
        $premiumInquiryVisitor->getExtInfoVisitor()->argc['busiId'] = $response->printerInit()->printer->argc['busiId'];

        $this->VehicleAndCustomerInfoConfirm($premiumInquiryVisitor);

        return $response;
    }

    /**
     * 车辆信和客户信息确认接口
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return void
     */
    private function VehicleAndCustomerInfoConfirm(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        $request = new VehicleAndCustomerInfoConfirmRequest();
        $request->setVisitor($premiumInquiryVisitor);
        $this->execute($request);
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return InsurancePriceQueryResponse
     * @throws Exception
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): InsurancePriceQueryResponse
    {
        //保费计算
        $request = new InsurancePriceQueryRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var InsurancePriceQueryResponse $response */
        $response = $this->execute($request);
        $response->translateCoverageCode($premiumInquiryVisitor->isNewEnergy);
        /**@var PremiumInquiryPrinter $printer */
        $printer = $response->getPrinter();

        if ($printer && $response->isSuccess() && $printer->success) {
            $riskType = $request->getMotorJointType();
            if ($riskType == '1') {
                $renewMark = $response->jqRenewMrk;
            } else {
                $renewMark = $response->syRenewMrk;
            }
            $startDate = new DateTime($premiumInquiryVisitor->getVehicleVisitor()->firstRegisterDate);
            $endDate = new DateTime(date('Y-m-d H:i:s'));
            $days = $startDate->diff($endDate)->days;

            if ($renewMark === '0') {
                if ($days > 270) {
                    $value = '转保';
                } else {
                    $value = '新保';
                }
            } else {
                $value = '续保';
            }

            $printer->premiumFactorPrinter->ComFactorAppend('商业忠诚度', 'BIRenewalType', $value);
            $printer->premiumFactorPrinter->ComFactorAppend('交强忠诚度', 'CIRenewalType', $value);

            $printer->businessType = $value;
            //非车回显
            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $totalPremium = 0;
            foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
                /*** @var $item NonCarProductVisitor */
                $single = new NonCarInquirySinglePrinter();
                $single->premium = $item->premium;
                $single->planCode = $item->planCode;
                $single->productCode = $item->productCode;
                $single->statusCode = 'success';
                $totalPremium += $single->premium * $item->insuredNum;
                $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
            $nonCarInquiryPrinter->totalPremium = $totalPremium;
            $printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
            $response->printer = $printer;
        }

        return $response;
    }

    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor)
    {

        //非车核保
        $request = new NonCarUnderWritingRequest();

        $nocarVisitor = $premiumInquiryVisitor->getNonCarUnderwritingVisitor();
        if ($nocarVisitor->getNonCarList()) {
            $nocarVisitor->argc['ciInquiryNo'] = $premiumInquiryVisitor->getUnderwritingVisitor()->ciInquiryNo;
            $nocarVisitor->argc['biInquiryNo'] = $premiumInquiryVisitor->getUnderwritingVisitor()->biInquiryNo;
            $request->setVisitor($nocarVisitor);
            $request->extVisitor = $premiumInquiryVisitor->getExtInfoVisitor();
            /*** @var NonCarUnderWritingResponse $noCarResponse */
            $noCarResponse = $this->execute($request);

            if (!$noCarResponse->isSuccess()) {
                return $noCarResponse;
            }
        }

        $request = new InsuranceConfirmRequest();
        $request->setVisitor($premiumInquiryVisitor);
        /**
         * @var InsuranceConfirmResponse $response
         */
        $response = $this->execute($request);
        /*** @var PremiumUnderwritingPrinter $printer */
        $printer = $response->getPrinter();
        if ($response->isSuccess() && $printer && isset($noCarResponse)) {
            //非车回填
            $printer->noCarUnderwritingPrinter = $noCarResponse->printer;
        }
        return $response;
    }

    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        //获取电子签名接口
        $request = new ElectronicSignQueryRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function uploadFile(UploadFileVisitor $uploadFileVisitor)
    {
        $request = new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);
        $imageList = $uploadFileVisitor->getFileList();
        $uploadFileVisitor->fileList = [];
        if (empty($imageList)) {
            throw new Exception("上传图片不能为空");
        }
        $response = '';
        foreach ($imageList as $val) {
            $upVisitor = new UploadFileVisitor();
            $upVisitor->argc = $uploadFileVisitor->argc;
            $upVisitor->ciInquiryNo = $uploadFileVisitor->ciInquiryNo;
            $upVisitor->biInquiryNo = $uploadFileVisitor->biInquiryNo;
            $upVisitor->inquiryNo = $uploadFileVisitor->inquiryNo;
            $upVisitor->fileAppend($val['type'], $val['url'], $val['base64']);
            $request->setVisitor($upVisitor);
            /*** @var UploadFileResponse $response */
            $response = $this->execute($request);
        }
        return $response;
    }

    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): OrderQueryResponse
    {
        $request = new OrderQueryRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var OrderQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): CancelOrderResponse
    {
        $request = new CancelOrderRequest();
        $request->setVisitor($revokeVisitor);

        /**
         * @var CancelOrderResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): ElectricalInsuranceResponse
    {
        //电子保单下载
        $request = new ElectronicInsuranceDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var ElectricalInsuranceResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): CancelOrderResponse
    {
        $request = new CancelOrderRequest();
        $request->setVisitor($recallVisitor);

        /*** @var CancelOrderResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): OrderQueryResponse
    {
        $request = new OrderQueryRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var OrderQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 验车码
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $request = new ValidateCarRequest();
        $request->setVisitor($validateCarVisitor);
        /*** @var ValidateCarResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------

    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {

        //$request = new NonCarUnderWritingRequest();
        //$request->setVisitor($nonCarProductListVisitor);
        //
        //$response = $this->execute($request);
        //return $response;

        $response = new NonCarProductListResponse();
        $response->seat = $nonCarProductListVisitor->seats;
        $response->printerInit();
        $response->setCode(200);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {

    }

    /**
     * 非车订单查询
     * @param NonCarPaymentVisitor $nonCarPaymentVisitor
     * @return NonCarOrderQueryResponse
     */
    public function nonCarOrderQuery(NonCarPaymentVisitor $nonCarPaymentVisitor)
    {
        $request = new NonCarOrderQueryRequest();
        $request->setVisitor($nonCarPaymentVisitor);

        /**
         * @var NonCarOrderQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 保险订单查询
     * @param $orderNo
     * @return InsuranceOrderQueryResponse
     */
    public function InsuranceOrderQuery($orderNo): InsuranceOrderQueryResponse
    {
        $request = new InsuranceOrderQueryRequest();
        $request->setOrderNo($orderNo);

        /*** @var InsuranceOrderQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function InsuranceOrderTrackQuery($sys, $beginDate, $endDate): CommonResponse
    {
        $request = new InsuranceOrderTrackQueryRequest();
        $request->setSysRes($sys);
        $request->setBeginDate($beginDate);
        $request->setEndDate($endDate);

        /*** @var CommonResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function BusinessOrderNoQuery($busiOpdpt, $busiOprCde, $vhlType): PreInquiryResponse
    {
        $request = new BusinessOrderNoQueryRequest();
        $request->setBusiOpdpt($busiOpdpt);
        $request->setBusiOprCde($busiOprCde);
        $request->setVhlType($vhlType);

        /*** @var PreInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function CarActualValueQuery($orderNo, $prodNo, $effectiveDate)
    {
        $request = new VehicleActualValueRequest();

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;

    }

    public function DeleteOrder($orderNo): CommonResponse
    {
        $request = new DeleteOrderRequest();
        $request->setOrderNo($orderNo);

        /*** @var CommonResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function UnIndividualPaymentApply($orderNo): CommonResponse
    {
        $request = new UnIndividualPaymentApplyRequest();
        $request->setOrderNo($orderNo);

        /*** @var CommonResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function OrderTrackQueryByOrderNo($orderNo)
    {
        $request = new OrderQueryRequest();
        $request->setOrderNo($orderNo);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function ElectronicSignFileQuery($proposalNo): CommonResponse
    {
        $request = new ElectronicSignFileQueryRequest();
        $request->setProposalNo($proposalNo);

        /*** @var CommonResponse $response */
        $response = $this->execute($request);
        return $response;
    }
}
